package com.milling.business.service.impl;

import java.util.List;

import com.alibaba.fastjson2.JSONObject;
import com.milling.common.utils.StringUtils;
import com.milling.common.utils.http.WmsApiUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.milling.business.mapper.WmsInStockMapper;
import com.milling.business.domain.WmsInStock;
import com.milling.business.service.IWmsInStockService;

/**
 * 入库信息Service业务层处理
 *
 * @author X
 * @date 2025-05-27
 */
@Service
public class WmsInStockServiceImpl implements IWmsInStockService
{
    private static final Logger logger = LoggerFactory.getLogger(WmsInStockServiceImpl.class);

    @Autowired
    private WmsInStockMapper wmsInStockMapper;

    /**
     * 查询入库信息
     *
     * @param id 入库信息主键
     * @return 入库信息
     */
    @Override
    public WmsInStock selectWmsInStockById(Long id)
    {
        return wmsInStockMapper.selectWmsInStockById(id);
    }

    /**
     * 查询入库信息列表
     *
     * @param wmsInStock 入库信息
     * @return 入库信息
     */
    @Override
    public List<WmsInStock> selectWmsInStockList(WmsInStock wmsInStock)
    {
        return wmsInStockMapper.selectWmsInStockList(wmsInStock);
    }

    /**
     * 新增入库信息
     *
     * @param wmsInStock 入库信息
     * @return 结果
     */
    @Override
    public int insertWmsInStock(WmsInStock wmsInStock, String jsonString)
    {
//        return wmsInStockMapper.insertWmsInStock(wmsInStock);
        WmsApiUtils wmsApiUtils = new WmsApiUtils();
        String result = wmsApiUtils.sortingInStock(jsonString);
        if(StringUtils.isNotEmpty(result)) {
            JSONObject jsonObject2 = JSONObject.parseObject(result);

            // 提取顶层字段
            String code = jsonObject2.getString("Code");
            String message = jsonObject2.getString("Message");

            if("0".equals(code)) {
                logger.info("通知WMS分拣入库钻针信息成功：" + message);
                return wmsInStockMapper.insertWmsInStock(wmsInStock);
            } else {
                logger.info("通知WMS分拣入库钻针信息失败，错误信息：" + message);
                return 0;
            }
        } else {
            return 0;
        }
    }

    /**
     * 修改入库信息
     *
     * @param wmsInStock 入库信息
     * @return 结果
     */
    @Override
    public int updateWmsInStock(WmsInStock wmsInStock)
    {
        return wmsInStockMapper.updateWmsInStock(wmsInStock);
    }

    /**
     * 批量删除入库信息
     *
     * @param ids 需要删除的入库信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsInStockByIds(Long[] ids)
    {
        return wmsInStockMapper.deleteWmsInStockByIds(ids);
    }

    /**
     * 删除入库信息信息
     *
     * @param id 入库信息主键
     * @return 结果
     */
    @Override
    public int deleteWmsInStockById(Long id)
    {
        return wmsInStockMapper.deleteWmsInStockById(id);
    }
}
